package com.inetlab.carnetvisualizer.controller;

import com.alibaba.fastjson.JSONObject;
import com.inetlab.carnetvisualizer.common.result.Result;
import com.inetlab.carnetvisualizer.entity.ServerResult;
import com.inetlab.carnetvisualizer.utils.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "车联网服务器数据接口")
@RequestMapping("/result")
@CrossOrigin
public class ServerResultController {
    @Autowired
    RedisTemplate<String, ServerResult> serverResultRedisTemplate;

    @ApiOperation("自动生成数据")
    @GetMapping("generate")
    public Result generateResult(){
        ServerResult serverResult1 = new ResultGenerator().generate("Alice", "114");
        ServerResult serverResult2 = new ResultGenerator().generate("Alice", "115");
        ServerResult serverResult3 = new ResultGenerator().generate("Alice", "116");
        ServerResult serverResult4 = new ResultGenerator().generate("Bob", "114");
        ServerResult serverResult5 = new ResultGenerator().generate("Bob", "115");
        ServerResult serverResult6 = new ResultGenerator().generate("Bob", "116");
        ArrayList<ServerResult> serverResults = new ArrayList<>();
        serverResults.add(serverResult1);
        serverResults.add(serverResult2);
        serverResults.add(serverResult3);
        serverResults.add(serverResult4);
        serverResults.add(serverResult5);
        serverResults.add(serverResult6);
        return Result.ok(serverResults);
    }
    @ApiOperation("从redis拉取实时监测数据")
    @GetMapping("queryRedisResult")
    public Result queryRedisResult(){
        Set<String> keys = serverResultRedisTemplate.keys("host:*");
        assert keys != null;
        List<ServerResult> serverResults = serverResultRedisTemplate.opsForValue().multiGet(keys);
        return Result.ok(serverResults);
    }
}
